<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}


class ad_members
{
	var $ipsclass;

	function auto_run()
	{
		$redirect = array(
									'lookup' => 'look_up',
									'xml_edit' => 'edit_page',
									'save' => 'save_member',
									'inventory' => 'edit_inventory',
									'add' => 'add_inventory',
										);
		if(!$redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'lookup';
		}
		$run = $redirect[$this->ipsclass->input['do']];
		$this->$run();
	}

	function look_up()
	{
		$this->ipsclass->admin->page_title  = "Welcome to member look up page the ibMarket";
		$this->ipsclass->admin->page_detail = "Here you can edit a member for ibMarket section";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=members', 'Manage Members' );

		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(), 'memberlookup', 'onsubmit="return false;"');

		$this->ipsclass->html .= "<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:100'></div>\n";

		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "60%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "40%" );

		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Look Up a Member");

		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic('Look Up a Member', 'left', 'tablesubheader');
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row( array("<b>What member do you want to look up?</b><br />Type in the first letters of the member's name then click.",
																			  $this->ipsclass->adskin->form_input("member_name",urldecode($this->ipsclass->input['member_name']),'text',"autocomplete='off' style='width:210px' id='member_name'")
																	 )      );

		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Edit Member","onclick='check_member()'");

		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();

		$this->ipsclass->html .= "<br />\n";
		$this->ipsclass->html .= "<div id='edit_member_fields' style='width:100%'>\n";
		$this->ipsclass->html .= "</div>\n";
		
		$this->ipsclass->html .= "<script type=\"text/javascript\" src='{$this->ipsclass->vars['board_url']}/jscripts/ipb_xhr_findnames.js'></script>\n";
		$this->ipsclass->html .= "<script type=\"text/javascript\">\n";
		$this->ipsclass->html .= "	init_js( 'memberlookup', 'member_name');\n";
		$this->ipsclass->html .= "	var tmp = setTimeout( 'main_loop()', 10 );\n";
		$this->ipsclass->html .= "	check_member();\n";
		$this->ipsclass->html .= "</script>\n";

		$this->ipsclass->admin->output();
	}

	function edit_page()
	{
		$ajax = $this->ipsclass->load_class(KERNEL_PATH.'class_ajax.php',"class_ajax");
		$ajax->class_init();
		
		$html = "";
		
		$name = $ajax->convert_and_make_safe($this->ipsclass->input['member_name'],0);
		if(strstr($this->ipsclass->input['member_name'], '%u2122'))
		{
			$name = $ajax->convert_and_make_safe(str_replace('%u2122','�',$this->ipsclass->input['member_name']),0);
		}
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".$this->ipsclass->DB->add_slashes( strtolower($name) )."'"));
		if(!$this->ipsclass->DB->get_num_rows() && strstr($this->ipsclass->input['member_name'], '%u2122'))
		{
			$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".$this->ipsclass->DB->add_slashes( strtolower(str_replace('�','%8482',$ajax->convert_and_make_safe($this->ipsclass->input['member_name'],0))) )."'"));
		}
		if(!$this->ipsclass->DB->get_num_rows())
		{
			$print = $this->ipsclass->adskin->start_table("Error");
			$print .= $this->ipsclass->adskin->add_td_row( array("Cannot find a member with that name.") );
			$print .= $this->ipsclass->adskin->end_table();
			echo $print;
			exit();
		}
		
		$html .= "<form></form>";
		
		$html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'section'   , 'components'  ),
															 2 => array( 'act'       , 'market'      ),
															 3 => array( 'code'      , 'members'     ),
															 4 => array( 'do'        , 'save'        ),
															 6 => array( 'member_id' , $member['id'] ),
													 )		);
		
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "40%" );
		$this->ipsclass->adskin->td_header[] = array( "{none}"  , "60%" );
		
		$html .= $this->ipsclass->adskin->start_table("Member Details");
		
		$html .= $this->ipsclass->adskin->add_td_row( array( "<b>Allow this member access to the Market?</b><br />By selecting yes or no, this will overwrite group settings." ,
														$this->ipsclass->adskin->form_dropdown('market_access',array(array(-1,'Use Group Setting'),array(0,'No'),array(1,'Yes')),$member['market_access'])
									     )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array( "<b>".$this->ipsclass->vars['market_currency']."</b>" ,
														$this->ipsclass->numbers->currency_format($this->ipsclass->adskin->form_simple_input( $this->ipsclass->vars['market_members_field'],
																		   $member[$this->ipsclass->vars['market_members_field']],10
																		  ))
									     )      );
		
		$html .= $this->ipsclass->adskin->add_td_row( array( "<b>Member Tax</b><br />This will add on to any tax in the items in the shop" ,
														$this->ipsclass->adskin->form_input( 'market_tax', $member['market_tax'] )
									     )      );
		
		$html .= $this->ipsclass->adskin->end_form("Save member details");
		
		$html .= $this->ipsclass->adskin->end_table();
		
		$html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'section'   , 'components'  ),
															 2 => array( 'act'       , 'market'      ),
															 3 => array( 'code'      , 'members'     ),
															 4 => array( 'do'        , 'inventory'   ),
															 6 => array( 'member_id' , $member['id'] ),
													 )      );
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"            , "" );
		$this->ipsclass->adskin->td_header[] = array( "Name & Description"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "Time Bought"         , "" );
		$this->ipsclass->adskin->td_header[] = array( "Bought/Resell Price" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Delete"              , "" );
		
		$html .= $this->ipsclass->adskin->start_table("Edit Member Inventory");
		
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"member_id=".$member['id'],'order'=>'i_id DESC'));
		$q = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($q))
		{
			if(!is_array($this->ipsclass->cache['market_items'][$item['item']]))
			{
				$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_inventory','where'=>"i_id=".$item['i_id']));
				continue;
			}
			
			$html .= $this->ipsclass->adskin->add_td_row( array(
																	"<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$this->ipsclass->cache['market_items'][$item['item']]['icon']."' border='0' alt='Item Icon' />",
																	$this->ipsclass->cache['market_category'][$this->ipsclass->cache['market_items'][$item['item']]['category']]['cat_name'],
																	"<b>".$this->ipsclass->cache['market_items'][$item['item']]['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item['item']]->parse_single_description($item))),
																	$this->ipsclass->get_date($item['time_bought'],'TINY'),
																	$this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input('price['.$item['i_id'].']',$item['price_bought']).$this->ipsclass->vars['market_suffix']."/<br />".$this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input('resell['.$item['i_id'].']',$item['resell_price']).$this->ipsclass->vars['market_suffix'],
																	$this->ipsclass->adskin->form_checkbox('delete['.$item['i_id'].']')
										     			)      );
		}
		
		$html .= $this->ipsclass->adskin->end_form("Save Member Inventory");
		
		$html .= $this->ipsclass->adskin->end_table();
		
		$html .= $this->ipsclass->adskin->start_form( array( 1 => array( 'section'   , 'components'  ),
															 2 => array( 'act'       , 'market'      ),
															 3 => array( 'code'      , 'members'     ),
															 4 => array( 'do'        , 'add'         ),
															 6 => array( 'member_id' , $member['id'] ),
													 )      );
		
		$this->ipsclass->adskin->td_header[] = array( "Icon"                , "1%" );
		$this->ipsclass->adskin->td_header[] = array( "Category"            , "" );
		$this->ipsclass->adskin->td_header[] = array( "Name & Description"  , "" );
		$this->ipsclass->adskin->td_header[] = array( "Bought/Resell Price" , "" );
		$this->ipsclass->adskin->td_header[] = array( "Quanity"             , "" );
		
		$html .= $this->ipsclass->adskin->start_table("New Inventory Items");
		
		$null = array();
		foreach($this->ipsclass->cache['market_items'] as $item_id => $item)
		{
			if(!$this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($null,1))
			{
				continue;
			}
			
			$html .= $this->ipsclass->adskin->add_td_row( array(
																	"<img src='".$this->ipsclass->market_class->image_url_path."/icons/".$item['icon']."' border='0' alt='Item Icon' />",
																	$this->ipsclass->cache['market_category'][$item['category']]['cat_name'],
																	"<b>".$item['item_name']."</b><br />".$this->ipsclass->parser->pre_display_parse($this->ipsclass->parser->pre_db_parse($this->ipsclass->market_class->items_loaded[$item_id]->parse_store_description())),
																	$this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input('price_bought['.$item_id.']',$item['price']).$this->ipsclass->vars['market_suffix']."/<br />".$this->ipsclass->vars['market_prefix'].$this->ipsclass->adskin->form_simple_input('resell_price['.$item_id.']',$item['resell']).$this->ipsclass->vars['market_suffix'],
																	$this->ipsclass->adskin->form_simple_input('quanity['.$item_id.']',0)
										     			)      );
		}
		
		$html .= $this->ipsclass->adskin->end_form("Add to Member Inventory");
		
		$html .= $this->ipsclass->adskin->end_table();
		
		$ajax->return_html($html);
	}

	function save_member()
	{
		$this->ipsclass->input['member_id'] = intval($this->ipsclass->input['member_id']);
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'name,members_display_name','from'=>'members','where'=>"id=".$this->ipsclass->input['member_id']));
		
		if(!$this->ipsclass->DB->get_num_rows())
		{
			$this->ipsclass->admin->error("Unable to find the member that is being edited.");
		}
		
		$this->ipsclass->input[$this->ipsclass->vars['market_members_field']] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input[$this->ipsclass->vars['market_members_field']],'int');
		
		$update = array(
$this->ipsclass->vars['market_members_field'] => $this->ipsclass->input[$this->ipsclass->vars['market_members_field']],
							'market_tax'      => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['market_tax']),
							'market_access'   => $this->ipsclass->input['market_access'],
						);
		
		$this->ipsclass->DB->do_update('members',$update,"id=".$this->ipsclass->input['member_id']);
		
		$this->ipsclass->admin->save_log("ibMarket: Edit member ".$member['members_display_name']);
		
		$this->ipsclass->main_msg = "Editing ".$member['name']." done.";
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=members&do=lookup&member_name=".urlencode($member['members_display_name']),"Done editing the member, loading edit page");
	}
	
	function edit_inventory()
	{
		$this->ipsclass->input['member_id'] = intval($this->ipsclass->input['member_id']);
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'name,members_display_name','from'=>'members','where'=>"id=".$this->ipsclass->input['member_id']));
		
		if(!$this->ipsclass->DB->get_num_rows())
		{
			$this->ipsclass->admin->error("Unable to find the member that is being edited.");
		}
		
		$delete = array();
		$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_inventory','where'=>"member_id=".$this->ipsclass->input['member_id'],'order'=>'i_id DESC'));
		$items = $this->ipsclass->DB->exec_query();
		while($item = $this->ipsclass->DB->fetch_row($items))
		{
			if(!isset($this->ipsclass->input['price'][$item['i_id']]) or !isset($this->ipsclass->input['resell'][$item['i_id']]))
			{
				continue;
			}
			if($this->ipsclass->input['delete'][$item['i_id']])
			{
				$delete[] = $item['i_id'];
				continue;
			}
			
			$this->ipsclass->DB->do_update('market_inventory',array(
																		'price_bought' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['price'][$item['i_id']],'int'),
																		'resell_price' => $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['resell'][$item['i_id']],'int'),
											),"i_id=".$item['i_id']);
		}
		
		if(count($delete))
		{
			$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_inventory','where'=>"i_id IN (".implode(", ",$delete).")"));
		}
		
		$this->ipsclass->admin->save_log("ibMarket: Edit ".$member['members_display_name']." member inventory");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=members&do=lookup&member_name=".urlencode($member['members_display_name']),"Done editing the member inventory, loading edit page");
	}
	
	function add_inventory()
	{
		$this->ipsclass->input['member_id'] = intval($this->ipsclass->input['member_id']);
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'id,name,members_display_name','from'=>'members','where'=>"id=".$this->ipsclass->input['member_id']));

		if(!$this->ipsclass->DB->get_num_rows())
		{
			$this->ipsclass->admin->error("Unable to find the member that is being edited.");
		}
		
		$time = time();
		
		foreach(array_keys($this->ipsclass->cache['market_items']) as $item_id)
		{
			if(!isset($this->ipsclass->input['price_bought'][$item_id]) or !isset($this->ipsclass->input['resell_price'][$item_id]) or !isset($this->ipsclass->input['quanity'][$item_id]))
			{
				continue;
			}
			$this->ipsclass->input['price_bought'][$item_id] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['price_bought'][$item_id],'int');
			$this->ipsclass->input['resell_price'][$item_id] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['resell_price'][$item_id],'int');
			$this->ipsclass->input['quanity'][$item_id] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['quanity'][$item_id],'int');
			if($this->ipsclass->input['quanity'][$item_id] <= 0)
			{
				continue;
			}
			
			for($i = 0; $i < $this->ipsclass->input['quanity'][$item_id]; $i++)
			{
				$inv_item = array(
									'member_id' => $member['id'],
									'item' => $item_id,
									'price_bought' => $this->ipsclass->input['price_bought'][$item_id],
									'resell_price' => $this->ipsclass->input['resell_price'][$item_id],
									'time_bought' => $time,
									'settings' => '',
								);
				if($this->ipsclass->market_class->items_loaded[$item_id]->public_on_create_item($inv_item,1))
				{
					$this->ipsclass->DB->do_insert('market_inventory',$inv_item);
				}
			}
		}
		
		$this->ipsclass->admin->save_log("ibMarket: Add to ".$member['members_display_name']." member inventory");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=members&do=lookup&member_name=".urlencode($member['members_display_name']),"Done adding to member inventory, loading edit page");
	}
}
?>